package com.xw.Service.impl;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import com.xw.Model.MailServerModel;
import com.xw.Model.api.ClusterInfoModel;
import com.xw.Service.ServerStateService;

@Service
public class ServerStateServiceImpl implements ServerStateService {
	
	private static final Logger logger = LoggerFactory.getLogger(ServerStateServiceImpl.class);

	@Autowired
	private RedisTemplate<String, MailServerModel> redisTemplate;
	@Value("${redis.mailService.clusterMapKey}")
	private String clusterMapKey;		//集群机器map key
	@Value("${redis.mailService.nowFatchingServer}")
	private String nowFatchingServer;
	@Value("${redis.mailService.clusterServerNameType}")
	private String clusterServerNameType;		//集群环境下服务器的名字类型，默认0，使用iP+name生成，1表示自定义配置
	@Value("${redis.mailService.clusterServerName}")
	private String clusterServerName;			//上面的配置为1的情况下，该配置才会生效
	
	
	@Override
	public MailServerModel getNowFatchingServer() {
		ValueOperations<String, MailServerModel> opsForValue = redisTemplate.opsForValue();
		return opsForValue.get(nowFatchingServer);
	}

	@Override
	public Map<String, MailServerModel> getClusterServers() {
		HashOperations<String, String, MailServerModel> opsForHash = redisTemplate.opsForHash();
		return opsForHash.entries(clusterMapKey);
	}

	@Override
	public MailServerModel getClusterServerInfoByName(String name) {
		HashOperations<String, String, MailServerModel> opsForHash = redisTemplate.opsForHash();
		return opsForHash.get(clusterMapKey, name);
	}

	@Override
	public void updateClusterServerInfoByName(String ipAndName, MailServerModel mailServerModel) {
		HashOperations<String,String, MailServerModel> opsForHash = redisTemplate.opsForHash();
		opsForHash.put(clusterMapKey, mailServerModel.ipAndName(), mailServerModel);
	}

	@Override
	public void updateNowFatchingServerInfo(MailServerModel msm) {
		redisTemplate.opsForValue().set(nowFatchingServer,msm);
	}

	@Override
	public void deleteClusterServerInfoByName(String ipAndName) {
		HashOperations<String, String, MailServerModel> opsForHash = redisTemplate.opsForHash();
		opsForHash.delete(clusterMapKey, ipAndName);
	}

	@Override
	public MailServerModel getServerModel() {
		MailServerModel sia = new MailServerModel();
		if ("0".equals(clusterServerNameType)) {
			//默认配置
			try {
				InetAddress ia =  InetAddress.getLocalHost();
				String hostName = ia.getHostName();
				String hostAddress = ia.getHostAddress();
				sia.setIp(hostAddress);
				sia.setName(hostName);
			} catch (UnknownHostException e) {
				logger.error("获取ip地址异常！",e);
			}
			return sia;
		}else{
			sia.setIp("");
			sia.setName(clusterServerName);
			return sia;
		}
	}

	@Override
	public ClusterInfoModel getClusterInfo() {
		ClusterInfoModel cim = new ClusterInfoModel();
		cim.setNowFatchingServer(this.getNowFatchingServer());
		Map<String, MailServerModel> clusterServersMap = this.getClusterServers();
		List<MailServerModel> clusterServers = new ArrayList<>(clusterServersMap.values());
		cim.setClusters(clusterServers);
		return cim;
	}

}
